from ..service.knowledge_redis import redis_client
import redis
import logging
from ..utils import AgentException


logger = logging.getLogger("chat_app")


REFRESH_TOKEN = "refresh_token"
NEW_ACCESS_TOKEN = "new_access_token"
# 过期时间一天
EXPIRE_TIME = 3600 * 24


def get_refresh_token(access_token: str) -> str:
    """
    从 Redis 中根据 access_token 获取 refresh_token

    参数:
        access_token (str): 访问令牌的键

    返回:
        refresh_token
    """
    try:
        # 获取 Redis 客户端实例
        client = redis_client()

        return client.hget(access_token, REFRESH_TOKEN)

    except redis.RedisError as e:
        logger.error(f"Redis 获取 refresh_token失败: {e}")
        raise AgentException("INTERNAL_ERROR", "获取 refresh_token失败")
    except Exception as e:
        logger.error(f"获取 refresh_token时发生意外错误: {e}")
        raise AgentException("INTERNAL_ERROR", "获取 refresh_token失败")


def get_new_access_token(access_token: str) -> str:
    """
    从 Redis 中根据 access_token 获取 new_access_token

    参数:
        access_token (str): 访问令牌的键

    返回:
        new_access_token 新生成的访问令牌token
    """
    try:
        # 获取 Redis 客户端实例
        client = redis_client()

        return client.hget(access_token, NEW_ACCESS_TOKEN)

    except redis.RedisError as e:
        logger.error(f"Redis 获取new_access_token失败: {e}")
        raise AgentException("INTERNAL_ERROR", "获取 new_access_token失败")
    except Exception as e:
        logger.error(f"获取 new_access_token时发生意外错误: {e}")
        raise AgentException("INTERNAL_ERROR", "获取 new_access_token失败")


def set_refresh_token(access_token: str, refresh_token: str) -> bool:
    """
    将刷新token存储到 Redis 中

    参数:
        access_token (str): 访问令牌的键
        refresh_token (str): 刷新令牌的token

    返回:
        bool: 如果存储成功则返回 True，否则返回 False
    """
    try:
        client = redis_client()
        result = client.hset(access_token, REFRESH_TOKEN, refresh_token)
        # 设置过期时间
        client.expire(access_token, EXPIRE_TIME)

        return result > 0
    except redis.RedisError as e:
        logger.error(f"Redis 存储 refresh_token失败: {e}")
        raise AgentException("INTERNAL_ERROR", "存储 refresh_token失败")
    except Exception as e:
        logger.error(f"存储 refresh_token时发生意外错误: {e}")
        raise AgentException("INTERNAL_ERROR", "存储 refresh_token失败")


def set_new_token(access_token: str, new_access_token: str) -> bool:
    """
    将 新的new_access_token存储到 Redis 中

    参数:
        access_token (str): 访问令牌的键
        new_access_token (str): 新的访问令牌token

    返回:
        bool: 如果存储成功则返回 True，否则返回 False
    """
    try:
        client = redis_client()
        result = client.hset(access_token, NEW_ACCESS_TOKEN, new_access_token)
        # 设置过期时间）
        client.expire(access_token, EXPIRE_TIME)

        return result > 0
    except redis.RedisError as e:
        logger.error(f"Redis 存储 new_access_token失败: {e}")
        raise AgentException("INTERNAL_ERROR", "存储 new_access_token失败")
    except Exception as e:
        logger.error(f"存储 new_access_token时发生意外错误: {e}")
        raise AgentException("INTERNAL_ERROR", "存储 new_access_token失败")
